package com.carkeeper.shop.service.impl;

import com.carkeeper.shop.pojo.ServiceType;
import com.carkeeper.shop.check.dao.TypeMapper;
import com.carkeeper.shop.service.TypeItemService;
import com.carkeeper.utile.IdWorker;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class TypeItemServiceImpl implements TypeItemService {

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private IdWorker idWorker;
    /**
     * 根据父类id查询父类名称
     * @param id
     * @return
     */
    @Override
    public String findTypeNameById(String id) {
        ServiceType serviceType = typeMapper.selectByPrimaryKey(id);
        return serviceType.getName();
    }

    /**
     * 多条件分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<ServiceType> findPage(Map<String, Object> searchMap, int page, int size) {
            PageHelper.startPage(page,size);
            Example example = createExample(searchMap);
            return (Page<ServiceType>)typeMapper.selectByExample(example);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<ServiceType> findPage(String id,int page, int size) {
        PageHelper.startPage(page,size);
        Page<ServiceType> pageList = new Page<>();
        //先查询所有的数据
        List<ServiceType> serviceTypes = typeMapper.findAllTypeItem();
        for (ServiceType serviceType : serviceTypes) {
                //判断是否通用，通用则加入到集合进行展示     有可能此对象是通用并且id相同
                if (serviceType.getUniversal().equals("1")&&!serviceType.getParentId().equals(id)) {
                        pageList.add(serviceType);
                    }
                if (serviceType.getParentId().equals(id)){
                    pageList.add(serviceType);
                }

            }
        return pageList;
    }


    /**
     * 新增数据
     * @param serviceType
     * @param id
     */
    @Override
    public void addTypeItem(ServiceType serviceType, String id) {
        serviceType.setParentId(id);
        serviceType.setId(String.valueOf(idWorker.nextId()));

        //设置code的格式
        String code = serviceType.getCode();
        //获取所有的分类数据
        List<ServiceType> typeItemList = typeMapper.findAllTypeItem();

        if (typeItemList.size()==0){//判断是否是第一条数据
            code=code+1000010000;
        }else {
            //获取最后一个数据
            ServiceType lastServiceType = typeItemList.get(typeItemList.size() - 1);
            //正则表达式获取code中的数字
            Pattern pattern = Pattern.compile("[^0-9]");
            Matcher matcher = pattern.matcher(lastServiceType.getCode());
            String strNum = matcher.replaceAll("");
            int number = Integer.parseInt(strNum);
            //组成新的code
            code = code+(number+1);
        }

        serviceType.setCode(code);

        typeMapper.insertSelective(serviceType);
    }


    /**
     * 修改
     * @param serviceType
     */
    @Override
    public void updateTypeItem(ServiceType serviceType) {
        typeMapper.updateByPrimaryKeySelective(serviceType);
    }

    /**
     * 删除数据
     * @param id
     */
    @Override
    public void deleteTypeItem(String id) {
        ServiceType serviceType = typeMapper.selectByPrimaryKey(id);

        if (serviceType.getParentId()!=null) {//表示有子分类
            throw new RuntimeException("无法删除");
        }
        typeMapper.deleteByPrimaryKey(id);
    }


    //分页查询条件
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(ServiceType.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap.get("code")!=null&&!"".equals(searchMap.get("code"))) {
            criteria.orEqualTo("code",searchMap.get("code"));
        }
        if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))) {
            criteria.orEqualTo("name",searchMap.get("name"));
        }
        return example;
    }



}
